package be.tf.freemaind.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import be.tf.freemaind.dao.PostDAO;
import be.tf.freemaind.model.Post;

import com.mysql.jdbc.Statement;

/**
 * Servlet implementation class ListPostServlet
 */
@WebServlet("/ListPost")
public class ListPostServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Statement stm = (Statement) this.getServletContext().getAttribute("stm");
		String error=null;
		try {
			List<Post> posts = new PostDAO(stm).findAll();
			request.setAttribute("posts", posts);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			error=e.getMessage();
			request.setAttribute("error", error);
		}
		request.getRequestDispatcher("/jsp/ListPost.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	
	}

}
